﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Threading;
using betfairfind.Betfair.Global;


namespace betfairfind
{

    public static class Login
    {
        static public bool LOG = true;
        static public string SESSION_TOKEN = "";
        static public string username;
        static public string password;
        static private int softwareId = 82;    // Free Access API
        static private int productId = 0;



        static public Thread KeepAliveThread;
        static int KeepAliveCycle = 1000 * 60 * 18;         // keep alive time  18 minutes
    //    static int KeepAliveCycle = 1000 * 5;         // keep alive time  15 seconds

        static private betfairfind.Betfair.Global.BFGlobalService m_globalService;
    //    static public APIRequestHeader API_REQUEST_HEADER = new APIRequestHeader();

        static  Login()
        {

            m_globalService = new betfairfind.Betfair.Global.BFGlobalService();


        }

        public static void ReuseLogin(string sesstoken) {
                SESSION_TOKEN = sesstoken;
                BF.HEADER_GLOBAL.sessionToken = sesstoken;
                BF.HEADER_UK.sessionToken = sesstoken;
        }

        static  public void DoLogin()
        {
            if (LOG)
            {
     //           Logger.Log("Trying to log in.", true, true);
            }

            LoginReq req = new LoginReq();
            LoginResp resp = new LoginResp();
            req.username = username;
            req.password = password;
            req.productId = softwareId;
            req.vendorSoftwareId = productId;

            try
            {
                resp = m_globalService.login(req);
            }
            catch (Exception e)
            {
                if (LOG)
                {
                    Logger.Log("Log in failed, " + resp.errorCode.ToString(), true, true);
                    Logger.Log("Log in failed, " + e.ToString(), true, true);
                    Logger.Log("Press any key to exit", false, true);
                    Console.ReadKey();
                    System.Environment.Exit(-1);
                }
            }

            //if (LOG)
            //{
            //    Logger.Log("Log in  " + resp.errorCode.ToString(), true, true);
            //}
            if (resp.errorCode.ToString() == "OK")
            {
                Logger.Log("Log in  " + resp.errorCode.ToString(), true, true);
                SESSION_TOKEN = resp.header.sessionToken;
                BF.HEADER_GLOBAL.sessionToken = SESSION_TOKEN;
                BF.HEADER_UK.sessionToken = SESSION_TOKEN;
                //BFA.HEADER_AU.sessionToken = SESSION_TOKEN;

                // start keep alive
                KeepAliveThread = new Thread(new ThreadStart(KeepAlive));
                KeepAliveThread.Name = "KeepAliveThread";
                KeepAliveThread.Start();

            }
            else
            {
                Logger.Log("Log in failed, " + resp.errorCode.ToString(), true, true);
                Logger.Log("Press any key to exit", false, true);
                Console.ReadKey();
                System.Environment.Exit(-1);
            }

          

        }


        static private void KeepAlive()
        {
            while (true)
            {
     
                Thread.Sleep(KeepAliveCycle);
                try
                {
                    
                    KeepAliveReq req = new KeepAliveReq();
                    req.header = BF.HEADER_GLOBAL;
                    KeepAliveResp resp = m_globalService.keepAlive(req);

                    SESSION_TOKEN = resp.header.sessionToken;
                  //  BFA.HEADER_AU = resp.header.sessionToken;
                   // BF.HEADER_UK = resp.header.sessionToken;
                    if (LOG)
                    {
                      //  Logger.Log("Login keepalive: new token=" + SESSION_TOKEN, true, true);
                        Logger.Log("Login keepalive ping", false, true);
                    }
                }
                catch (Exception ex)
                {
                    if (LOG)
                    {
                        Logger.Log(ex.ToString(), true, true);
                    }
                }
            }



        }



    }
}
